package com.liang.controller;

import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.swing.JOptionPane;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liang.pojo.Gorillaceo;
import com.liang.pojo.OrderItem;
import com.liang.pojo.Pingjia;
import com.liang.service.OrderItemService;
import com.liang.service.OrdersService;
import com.liang.service.PingjiaService;
import com.liang.service.UserService;

@Controller
public class PingjiaController {
	@Autowired
	PingjiaService pingjiaService;
	@Autowired
	UserService userService;
	@Autowired
	OrderItemService orderItemService;
	@Autowired
	OrdersService ordersService;
	
	
	@ResponseBody
	@RequestMapping("/pingjia/pingjia")
	public int pingjia(@RequestParam("sid")int sid,HttpServletRequest request) {
		List<Pingjia> list = pingjiaService.selectAllPingjiaBySid(sid);
		for (Pingjia pingjia : list) {
			if(pingjia.getUid()==(int) request.getSession().getAttribute("uid")) {
				return 1;
			}
		}
		return 0;
	}
	@RequestMapping("/pingjia/pingjiaa")
	public String pingjiaa(@RequestParam("oid")int oid,@RequestParam("sid")int sid,Model model) {
		model.addAttribute("oid", oid);
		model.addAttribute("sid", sid);
		return "pingjia/pingjia";
	}
	
	@ResponseBody
	@RequestMapping("/pingjia/insertPingjia")
	public int insertPingjia(Pingjia pingjia,HttpServletRequest request) {
		System.out.println(pingjia);
		int uid=(int) request.getSession().getAttribute("uid");
		if(pingjia.getContent().trim()=="" || pingjia.getContent().isEmpty()) {
			pingjia.setContent("未输入评价");
		}
		if(pingjia.getStar()<=1) {
			pingjia.setStar(1);
		}
		if(pingjia.getStar()>5) {
			pingjia.setStar(5);
		}
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("sid", pingjia.getSid());map.put("uid", uid);map.put("star", pingjia.getStar());
		map.put("content", pingjia.getContent());map.put("pingjiaTime", DateFormat.getDateTimeInstance().format(new Date()));
		map.put("oid", pingjia.getOid());map.put("orderTime", ordersService.selectOrdersByOid(pingjia.getOid()).getOrderTime());
		System.out.println(map);
		int i = pingjiaService.insertPingjia(map);
		OrderItem orderItem = orderItemService.selectOrderItemByOidAndSid(pingjia.getOid(), pingjia.getSid());
		orderItem.setState(1);
		orderItemService.updateOrderItem(orderItem);
		return i;
	}
	
	@ResponseBody
	@RequestMapping("/pingjia/selectAllPingjiaBySid")
	public Object selectAllPingjiaBySid(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,HttpServletRequest request,
			@RequestParam("sid")int sid) {
		// 这个必须放在查询条件的前面，最好第一行
		PageHelper.startPage(pageNum, 12);
		
		List<Pingjia> list = pingjiaService.selectAllPingjiaBySid(sid);
		for (Pingjia pingjia : list) {
			String username = userService.selectUserById(pingjia.getUid()).getUsername();
			pingjia.setUsername(username);
		}
		PageInfo<Pingjia> pageInfo = new PageInfo<Pingjia>(list);
		return pageInfo;
	}
	@ResponseBody
	@RequestMapping("/pingjia/deletePingjiaByList")
	public Object deleteBatch(Gorillaceo gorillaceo) {System.out.println(gorillaceo);
		int res = JOptionPane.showConfirmDialog(null, "是否继续删除", "是否继续", JOptionPane.YES_NO_OPTION);
		int i = 0;
		if (res == JOptionPane.YES_OPTION) {
			i = pingjiaService.deletePingjiaByList(gorillaceo); // 点击“是”后执行这个代码块
			
		} else {
			i = -1; // 点击“否”后执行这个代码块 }
		}
		return i;
	}
	@ResponseBody
	@RequestMapping("/pingjia/deletePingjiaByUser")
	public Object deletePingjiaByUser(Pingjia pingjia) {System.out.println(pingjia);
	int res = JOptionPane.showConfirmDialog(null, "是否继续删除", "是否继续", JOptionPane.YES_NO_OPTION);
	int i = 0;
	if (res == JOptionPane.YES_OPTION) {
		i = pingjiaService.deletePingjiaByUser(pingjia); // 点击“是”后执行这个代码块
	} else {
		i = -1; // 点击“否”后执行这个代码块 }
	}
	return i;
	}
}
